<?php
  /**
   * iJSessionManager
   *
   * The iJSessionManager class provides storin session to data base
   *
   * @copyright Copyright (c) 2011, iJEngine Progect
   */
   class iJSessionManager
   {
     /**
      *
      * Class Constructuctor
      * 
      * @access public
      */
      public function __construct() {
           session_set_save_handler (array($this, "_open"), 
                                     array($this, "_close"), 
                                     array($this, "_read"), 
                                     array($this, "_write"),
                                     array($this, "_destroy"),
                                     array($this, "_clean")); 
      }
      
      
     /**
      *
      * Session open function
      * 
      * @access public
      */
      public function  _open() {
        $iJDataBase =  iJFactory::singleton('iJDataBase');
        return $iJDataBase->isConncted();
      }
      
     /**
      *
      * Session close function
      * 
      * @access public
      */
      public function  _close() {
        return true;
      }
      

    /**
     *
     * Session _read function
     *  
     * @access public
     */
    function _read($id) { 
      $id = mysql_real_escape_string($id); 
      $iJDataBase =  iJFactory::singleton('iJDataBase');
      $QSession    =  $iJDataBase->query('SELECT 
                                           session_data
                                         FROM :table_session 
                                         WHERE id = :id
                                        ');
      $QSession->bindTable(':table_session',       TABLE_SESSION);
      $QSession->bindValue(':id',                  $id);
      $QSession->execute();
      if($QSession->next()) {
        return $QSession->value('session_data'); 
      } else {
        return '';
      }
    }
    
    /**
     *
     * Session _read function
     *  
     * @access public
     */
    function _write($id, $data) { 
      $iJDataBase =  iJFactory::singleton('iJDataBase');
      $QSession    =  $iJDataBase->query('REPLACE INTO :table_session 
                                           VALUES (:id, :session_data, :updated)
                                        ');
      $QSession->bindTable(':table_session',       TABLE_SESSION);
      $QSession->bindValue(':id',                  $id);
      $QSession->bindValue(':session_data',        $data);
      $QSession->bindValue(':updated',             time());
      $QSession->execute();
      return $iJDataBase->isError();
    } 
    
    
    /**
     *
     * Session _destroy function
     *  
     * @access public
     */
    function _destroy($id)
    { 
      $id = mysql_real_escape_string($id); 
      $iJDataBase =  iJFactory::singleton('iJDataBase');
      $QSession    =  $iJDataBase->query('DELETE FROM :table_session 
                                           WHERE id = :id
                                        ');
      $QSession->bindTable(':table_session',       TABLE_SESSION);
      $QSession->bindValue(':id',                  $id);
      $QSession->execute();
      return $iJDataBase->isError();
    } 
  
    function _clean($max) 
    { 
      $old = time() - $max; 
      $old = mysql_real_escape_string($old); 
      $iJDataBase =  iJFactory::singleton('iJDataBase');
      $QSession    =  $iJDataBase->query('DELETE FROM :table_session 
                                            WHERE updated < :old
                                        ');
      $QSession->bindTable(':table_session',       TABLE_SESSION);
      $QSession->bindValue(':old',                  $old);
      $QSession->execute();
      return $iJDataBase->isError();
    } 

    
    
    
    
  }


?>